﻿using CDV.MC.MatrixDriver.WebApp.Data;
using CDV.MC.MatrixDriver.WebApp.Extensions;
using CDV.MC.MatrixDriver.WebApp.Filter;
using CDV.MC.MatrixDriver.WebApp.Models;
using CDV.MC.MatrixDriver.WebApp.Service;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace CDV.MC.MatrixDriver.WebApp.Controllers
{

    public class MatrixController : Controller
    {
        private readonly IHttpClientProvider _httpClientProvider;

        public MatrixController(IServiceProvider serviceProvider)
        {
            _httpClientProvider = serviceProvider.GetService<IHttpClientProvider>();
        }

        [LoginFilter]
        public IActionResult Index(string clientIP)
        {
            ViewData["ClientIP"] = HttpContext.Session.GetString("ClientIP");
            return View();
        }

        public ActionResult Login(LoginModel loginModel)
        {
            return View(loginModel);
        }

        [LoginFilter]
        public ActionResult MatrixView()
        {
            using (var httpClient = _httpClientProvider.GetHttpClient(HttpContext))
            {
                var matrixInfo = httpClient.Get<IList<MatrixInfo>>("matrix", "GetMatrices");
                var matrixView = matrixInfo.OrderBy(matrix => matrix.ID);
                return View(matrixView);
            }
        }

        [LoginFilter]
        public ActionResult MatrixDetail(string matrixId)
        {
            using (var httpClient = _httpClientProvider.GetHttpClient(HttpContext))
            {
                //获取矩阵概要信息
                var matrixInfo = Task.Factory.StartNew(new Func<MatrixInfo>(() =>
                {
                    var matrixInfoList = httpClient.Get<IList<MatrixInfo>>("matrix", "GetMatrices", "matrixID=" + matrixId);
                    return matrixInfoList != null && matrixInfoList.Count > 0 ? matrixInfoList[0] : null;
                }));

                //result.ContinueWith()

                //获取矩阵出端口信息
                var destPortInfo = Task.Factory.StartNew(new Func<IList<MatrixPortInfo>>(() =>
                {
                    var matrixPortInfo = httpClient.Get<IList<MatrixPortInfo>>("matrix", "GetDestPorts", "matrixID=" + matrixId);
                    return matrixPortInfo;
                }));

                //获取矩阵源端口信息
                var srcPortInfo = Task.Factory.StartNew(new Func<IList<MatrixPortInfo>>(() =>
                {
                    var matrixPortInfo = httpClient.Get<IList<MatrixPortInfo>>("matrix", "GetSrcPorts", "matrixID=" + matrixId);
                    return matrixPortInfo;
                }));

                var matrixDetailInfo = new MatrixDetailModel()
                {
                    MatrixInfo = matrixInfo.Result,
                    DestPortInfo = destPortInfo.Result,
                    SrcPortInfo = srcPortInfo.Result
                };

                return View(matrixDetailInfo);
            }
        }

        [HttpPost]
        [LoginFilter]
        public bool Lock(int matrixId, int destPort)
        {
            using (var httpClient = _httpClientProvider.GetHttpClient(HttpContext))
            {
                var result = httpClient.Get<bool>("matrix", "Lock", $"matrixID={matrixId}&destPort={destPort}");

                return result;
            }
        }

        [HttpPost]
        [LoginFilter]
        public bool UnLock(int matrixId, int destPort)
        {
            using (var httpClient = _httpClientProvider.GetHttpClient(HttpContext))
            {
                var result = httpClient.Get<bool>("matrix", "UnLock", $"matrixID={matrixId}&destPort={destPort}");

                return result;
            }
        }


        [HttpPost]
        [LoginFilter]
        public bool Take(int matrixId, int destPort, int srcPort)
        {
            using (var httpClient = _httpClientProvider.GetHttpClient(HttpContext))
            {
                var result = httpClient.Get<bool>("matrix", "Take", $"matrixID={matrixId}&level=1&destPort={destPort}&srcPort={srcPort}");

                return result;
            }
        }
    }
}